﻿using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.byhand
{
    public class Sg560 : Information
    {
        string __VIEWSTATE = "", __EVENTVALIDATION = "";
        //登陆
        public override bool Login(string name, string pass, string vcode)
        {
            string html = source.Get("http://b2b.sg560.com/member/login_1.aspx");
            ParametersData pd = new ParametersData();
            pd.Add("ctl00$ScriptManager1", "ctl00$ContentPlaceHolder1$UpdatePanel1|ctl00$ContentPlaceHolder1$ImageBuEnter");
            pd.Add("__EVENTTARGET", "");
            pd.Add("__EVENTARGUMENT", "");
            pd.Add("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)\"").Groups[1].Value);
            pd.Add("__EVENTVALIDATION", Regex.Match(html, "id=\"__EVENTVALIDATION\" value=\"([^\"]+)\"").Groups[1].Value);
            pd.Add("ctl00$TextArea1", "");
            pd.Add("ctl00$ContentPlaceHolder1$tbUserName", name);
            pd.Add("ctl00$ContentPlaceHolder1$tbPassword", pass);
            pd.Add("ctl00$ContentPlaceHolder1$VAJADATA", vcode);
            pd.Add("ctl00$ContentPlaceHolder1$ImageBuEnter.x", "17");
            pd.Add("ctl00$ContentPlaceHolder1$ImageBuEnter.y", "22");

            html = source.Post("http://b2b.sg560.com/member/login_1.aspx?s=main&uname=" + name, pd.Read(), "http://b2b.sg560.com/member/login_1.aspx");
            if (html.IndexOf("text-decoration:underline;\">退出</a>") != -1)
            {
                return true;
            }
            return false;
        }
        //发布
        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            content = MyFunction.ToText(content);
            string html = source.Get("http://b2b.sg560.com/member/trade/SellTosr.aspx");
            __VIEWSTATE = Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)\"").Groups[1].Value;
            __EVENTVALIDATION = Regex.Match(html, "id=\"__EVENTVALIDATION\" value=\"([^\"]+)\"").Groups[1].Value;
            //上传图片
            UploadImage();
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("__EVENTTARGET", "");
            sfd.Write("__EVENTARGUMENT", "");
            sfd.Write("__LASTFOCUS", "");
            sfd.Write("__VIEWSTATE", __VIEWSTATE);
            sfd.Write("__EVENTVALIDATION", __EVENTVALIDATION);
            sfd.Write("ctl00$TextArea1", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sTypeList", "供应");
            sfd.Write("ctl00$ContentPlaceHolder1$sThemeTB", title);
            sfd.Write("ctl00$ContentPlaceHolder1$sKeyword1", ktag.Get());
            sfd.Write("ctl00$ContentPlaceHolder1$sKeyword2", ktag.Get());
            sfd.Write("ctl00$ContentPlaceHolder1$sKeyword3", ktag.Get());
            sfd.Write("ctl00$ContentPlaceHolder1$sTimelList", "三个月");
            sfd.Write("ctl00$ContentPlaceHolder1$siCate1", site.TypeId[0]);
            sfd.Write("ctl00$ContentPlaceHolder1$siCate2", site.TypeId[1]);
            sfd.Write("ctl00$ContentPlaceHolder1$siCate3", site.TypeId[2]);
            sfd.Write("ctl00$ContentPlaceHolder1$sUnitTB", pif.Get("单位"));
            sfd.Write("ctl00$ContentPlaceHolder1$sPriceTB", pif.Get("价格"));
            sfd.Write("ctl00$ContentPlaceHolder1$sNumTB", "1");
            sfd.Write("ctl00$ContentPlaceHolder1$sLeastNumTB", "1");
            sfd.Write("ctl00$ContentPlaceHolder1$sBrandTB", pif.Get("品牌"));
            sfd.Write("ctl00$ContentPlaceHolder1$sPackTB", pif.Get("包装"));
            sfd.Write("ctl00$ContentPlaceHolder1$sSpecTB", pif.Get("产品规格"));
            sfd.Write("ctl00$ContentPlaceHolder1$sTransitTB", pif.Get("运输方式"));
            sfd.Write("ctl00$ContentPlaceHolder1$sDeliveryTB", pif.Get("交货方式"));
            sfd.Write("ctl00$ContentPlaceHolder1$sDescribeTA", content);
            //发送文件
            sfd.WriteFile("ctl00$ContentPlaceHolder1$sFileUpload", "");
            //尾部数据
            //验证码
            //sfd.Write("ctl00$ContentPlaceHolder1$VAJADATA", vcode);
            sfd.Write("ctl00$ScriptManager1", "ctl00$ContentPlaceHolder1$UpdatePanel3|ctl00$ContentPlaceHolder1$sReleaseButton");
            sfd.Write("__AjaxFileUploading__", "__IsInAjaxFileUploading__");
            sfd.Write("ctl00$ContentPlaceHolder1$sReleaseButton", "发布");
            //结尾
            html = source.Post("http://b2b.sg560.com/member/trade/SellTosr.aspx", sfd.GetContentType(), sfd.Read(), "http://b2b.sg560.com/member/trade/SellTosr.aspx");
            if (html.IndexOf("su(1)|*/}</script>") != -1)
            {
                html = source.Get("http://b2b.sg560.com/member/trade/mSell,overpass.aspx");
                info.Code = 1;
                info.Content = Regex.Match(html, "href='(http://www.sg560.com/sells/[^']+)'>" + title + "</a>").Groups[1].Value;
            }
            else if (html.IndexOf("您今天已经发布了50条信息，请明天再发布") != -1)
            {
                info.Code = 3;
            }
            else
            {
                info.Content = Regex.Match(html, "alert\\('([^\']+)'\\)\\|\\*/}</script>").Groups[1].Value;
            }
            return info;
        }

        public void UploadImage()
        {
            string html = "";
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("__EVENTTARGET", "");
            sfd.Write("__EVENTARGUMENT", "");
            sfd.Write("__LASTFOCUS", "");
            sfd.Write("__VIEWSTATE", __VIEWSTATE);
            sfd.Write("__EVENTVALIDATION", __EVENTVALIDATION);
            sfd.Write("ctl00$TextArea1", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sTypeList", "供应");
            sfd.Write("ctl00$ContentPlaceHolder1$sThemeTB", "供应");
            sfd.Write("ctl00$ContentPlaceHolder1$sKeyword1", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sKeyword2", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sKeyword3", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sTimelList", "10天");
            sfd.Write("ctl00$ContentPlaceHolder1$siCate1", site.TypeId[0]);
            sfd.Write("ctl00$ContentPlaceHolder1$siCate2", site.TypeId[1]);
            sfd.Write("ctl00$ContentPlaceHolder1$siCate3", site.TypeId[2]);
            sfd.Write("ctl00$ContentPlaceHolder1$sUnitTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sPriceTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sNumTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sLeastNumTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sBrandTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sPackTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sSpecTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sTransitTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sDeliveryTB", "");
            sfd.Write("ctl00$ContentPlaceHolder1$sDescribeTA", "");
            //发送文件
            sfd.WriteFile("ctl00$ContentPlaceHolder1$sFileUpload", RandomPicture.Get());
            //尾部数据
            sfd.Write("ctl00$ContentPlaceHolder1$VAJADATA", "");
            sfd.Write("ctl00$ScriptManager1", "ctl00$ContentPlaceHolder1$UpdatePanel2|ctl00$ContentPlaceHolder1$sSendImageButton");
            sfd.Write("__AjaxFileUploading__", "__IsInAjaxFileUploading__");
            sfd.Write("ctl00$ContentPlaceHolder1$sSendImageButton", "发布");
            //结尾
            html = source.Post("http://b2b.sg560.com/member/trade/SellTosr.aspx", sfd.GetContentType(), sfd.Read(), "http://b2b.sg560.com/member/trade/SellTosr.aspx");
            __VIEWSTATE = Regex.Match(html, "__VIEWSTATE\\|([^\\|]+)").Groups[1].Value;
            __EVENTVALIDATION = Regex.Match(html, "__EVENTVALIDATION\\|([^\\|]+)").Groups[1].Value;
        }

        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://b2b.sg560.com/CheckCode.aspx", "http://b2b.sg560.com/member/trade/BuyInfo.aspx");
        }
    }
}
